package cn.hchaojie.blog.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

import cn.hchaojie.blog.domain.AjaxResponse;
import cn.hchaojie.blog.domain.Category;
import cn.hchaojie.blog.service.CategoryService;

/**
 * 添加分类
 * @author hchaojie
 *
 */
@WebServlet(urlPatterns = "/category_add")
public class CategoryAddServlet extends HttpServlet {
	CategoryService service = new CategoryService();
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String categoryName = req.getParameter("name");
		
		Category category = new Category();
		category.setName(categoryName);
		
		service.insert(category);
		
		// 返回一个简单的响应成功消息
		AjaxResponse reponse = new AjaxResponse();
		reponse.setCode(0);
		reponse.setMsg("success");
		
		String string = JSON.toJSONString(reponse);
		resp.setContentType("application/json;charset=utf-8");
		resp.getWriter().write(string);
	}
}
